package com.frogs.ucenter.v1.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserRechargeAddRequest;
import com.frogs.ucenter.v1.request.UserRechargeBalanceShowRequest;
import com.frogs.ucenter.v1.request.UserRechargeBalanceVerifyRequest;
import com.frogs.ucenter.v1.request.UserRechargeConsumeBackRequest;
import com.frogs.ucenter.v1.request.UserRechargeListRequest;
import com.frogs.ucenter.v1.request.UserRechargePayCallbackRequest;
import com.frogs.ucenter.v1.request.UserRechargeRecordInfoRequest;
import com.frogs.ucenter.v1.request.UserRechargeUpdateRequest;
import com.frogs.ucenter.v1.service.UserRechargeBalanceService;
import com.frogs.ucenter.v1.service.UserRechargeRecordService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userRechargeController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserRechargeController extends BaseController3 {
	
	private static final Logger log = LoggerFactory.getLogger(UserRechargeController.class);

	private static final String ROOT = "/user";
	private static final String USER_RECHARGE_ADD = ROOT + "/recharge/add";
	private static final String USER_RECHARGE_UPDATE = ROOT + "/recharge/update";
	private static final String USER_RECHARGE_LIST = ROOT + "/recharge/list";
	private static final String USER_RECHARGE_DETAIL = ROOT + "/recharge/detail";
	
	private static final String USER_RECHARGE_PAY_CALLBACK = "/recharge/payCallback";
	private static final String USER_RECHARGE_BALANCE_DETAIL = ROOT + "/recharge/balance" + DETAIL;
	
	private static final String USER_RECHARGE_BALANCE_VERIFY = ROOT + "/recharge/verify";
	private static final String USER_RECHARGE_CONSUME_BACK = ROOT + "/consume/back";
    
	@Resource(name=ApiConstants.VERSION+"userRechargeRecordService")
	private UserRechargeRecordService userRechargeRecordService;
	
	@Resource(name = ApiConstants.VERSION + "userRechargeBalanceService")
	private UserRechargeBalanceService userRechargeBalanceService;

	/**
	 * 创建充值记录
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_RECHARGE_ADD)
    public Response userRechargeRecordAdd(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserRechargeAddRequest userRechargeAddRequest = new UserRechargeAddRequest();
        	userRechargeAddRequest.parse(request);
        	response = userRechargeRecordService.addUserRechargeRecord(userRechargeAddRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 获取充值记录列表接口
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_RECHARGE_LIST)
    public Response userRechargeRecordList(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserRechargeListRequest userRechargeListRequest = new UserRechargeListRequest();
        	userRechargeListRequest.parse(request);
        	response = userRechargeRecordService.getUserRechargeRecordList(userRechargeListRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 查询充值记录信息接口
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping({USER_RECHARGE_DETAIL})
    public Response userRechargeDetail(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserRechargeRecordInfoRequest recordInfoRequest = new UserRechargeRecordInfoRequest();
        	recordInfoRequest.parse(request);
        	response = userRechargeRecordService.getUserRechargeRecordInfo(recordInfoRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 支付回调接口
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(USER_RECHARGE_PAY_CALLBACK)
    public String userRechargePayCallback(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		PrintWriter write = response.getWriter();
		Response apiResponse = null ; 
        try{
        	log.info("func[payCallback] request[" + JSON.toJSONString(request.getParameterMap()) + "] desc[pay center params]");
        	UserRechargePayCallbackRequest payCallbackRequest = new UserRechargePayCallbackRequest();
        	payCallbackRequest.parse(request);
        	apiResponse = userRechargeRecordService.payCallback(payCallbackRequest);
        	write.print("success");
        }catch (Exception e) {
        	apiResponse = new FailResponse();
			if (e instanceof AppBaseException) {
				apiResponse.setTip(e.getMessage());
			} else {
				apiResponse.setTip("errro message" + e.getMessage());
			}
			write.print("fail");
		}
        return null;
    }

	/**
	 * 获取用户余额
	 * @param httpRequest
	 * @return
	 */
	@RequestMapping({USER_RECHARGE_BALANCE_DETAIL})
	public Response userRechargeBalanceDetail(HttpServletRequest httpRequest) {
		Request request = new UserRechargeBalanceShowRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userRechargeBalanceService.showUserRechargeBalance(request);
			response.setTip("获取账户余额成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}

		return response;
	}
	
	/**
	 * 验证用户余额
	 * @param httpRequest
	 * @return
	 */
	@RequestMapping(USER_RECHARGE_BALANCE_VERIFY)
	public Response userRechargeBalanceVerify(HttpServletRequest httpRequest) {
		Request request = new UserRechargeBalanceVerifyRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userRechargeBalanceService.verifyUserRechargeBalance(request);
			response.setTip("验证余额成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 更新充值记录
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_RECHARGE_UPDATE)
    public Response updateUserRechargeRecord(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserRechargeUpdateRequest userRechargeUpdateRequest = new UserRechargeUpdateRequest();
        	userRechargeUpdateRequest.parse(request);
        	response = userRechargeRecordService.update(userRechargeUpdateRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 用户消费退款接口
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_RECHARGE_CONSUME_BACK)
	public Response userRechargeConsumeBack(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserRechargeConsumeBackRequest userRechargeConsumeBackRequest = new UserRechargeConsumeBackRequest();
			userRechargeConsumeBackRequest.parse(request);
			response = userRechargeRecordService.backConsume(userRechargeConsumeBackRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}

}
